<?php

namespace app\api\controller;


use app\helper\WxHelper;
use EasyWeChat\Factory;
use think\facade\App;
use think\facade\Env;
use think\facade\Log;

class Index extends Base {
    public function index() {
        $this->debug([
            'TpVersion' => App::version()
        ]);

        return $this->buildSuccess([
            'Product'    => config('apiadmin.APP_NAME'),
            'Version'    => config('apiadmin.APP_VERSION'),
            'Company'    => config('apiadmin.COMPANY_NAME'),
            'ToYou'      => "I'm glad to meet you（终于等到你！）"
        ]);
    }
    public function wxMsg(){
        $helper =  new  WxHelper();
        $wx=$helper->getWx(2);
        $wx->server->push(function ($message) {
            // $message['FromUserName'] // 用户的 openid
            // $message['MsgType'] // 消息类型：event, text....
            Log::error(json_encode($message));
            return "您好！欢迎使用 EasyWeChat";
        });

        $response = $wx->server->serve();

        return $response->send();
    }
    public function qywxMsg(){
        $config = [
            'corp_id' => 'ww73b8ab3e0adb6ec3',
//            'agent_id' => 100020, // 如果有 agend_id 则填写
            'secret'   => '9x_pWs1R9LwBELiBkAzy1VEdyB9m60AVKbkze5tkcak',
            'token' => 'dnG2DnYpm7kW1NOTw9aMPLjqb3iq',          // Token
            'aes_key' => 'D77JSmKO3dDqKJv31t4AZDdrENtsDfDBxNWzhyaArSr',
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            'log' => [
                'level' => 'debug',
                'file' => Env::get('root_path') . 'runtime/log/wechat/wechat_qywx.log',
            ],
        ];
        $qyWx = Factory::work($config);

        $qyWx->server->push(function ($message) {
            // $message['FromUserName'] // 用户的 openid
            // $message['MsgType'] // 消息类型：event, text....
            Log::error(json_encode($message));
            return "您好！欢迎使用 EasyWeChat";
        });

        $response = $qyWx->server->serve();

        return $response->send();
    }
    public function allow(){
        $serial= $this->request->get('serial', null);
        return 1;
    }
}
